﻿Imports System.Resources
Imports Microsoft.Win32
Imports System.IO

Public Class frmUtilityTracks
    Dim startRec As Image = My.Resources.Images.bout_enregistrer
    Dim stopRec As Image = My.Resources.Images.bout_pause
    Private rm As New ResourceManager(GetType(frmUtilityTracks))

    Private Sub BtnSaveTrack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSaveTrack.Click
        Dim DateNom As String = Now.ToString("yyMMdd")
        '*******************************************
        'sauvegarde du track s'il y a des données
        'Sauvegarde du tracé du GPS dans un fichier choisi/créé par l'utilisateur
        Dim wSaveFileDialog As New SaveFileDialog()
        Dim wFlag As Boolean
        wSaveFileDialog.Filter = "Fichiers GPX(gpx)|*.gpx|Tous les fichiers|*.*"
        If wSaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            wFlag = True
            SaveTrack(wSaveFileDialog.FileName, FormMain.NSDossier, False)
        End If
        wSaveFileDialog.Dispose()
        'on supprime le fichier de récupération des données du TRACK (fichier temporaire, sauvegarde TRACK TrackGPS_YYMMdd)
        File.Delete(FormMain.NSDossier & "TrackGPS_" & DateNom & ".gpx")
        MsgBox(DirectCast(My.Resources.Images.AlertSaveOk, String), MsgBoxStyle.Information, "Pocket eRelevé")

    End Sub

    Private Sub btn_Menu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Menu.Click
        FormMain.Show()
        Me.Hide()
    End Sub

    Private Sub btnRetour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetour.Click
        frmUtilityChoix.Show()
        Me.Hide()
    End Sub

    Private Sub BtnStopSaveTrack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStopSaveTrack.Click
        'Si l'enregistrement est en cours = Arreter l'enregistrement
        If Me.Txt_TrackStatus.Text = "true" Then
            TurnAutomaticPowerOff(True)
            Me.Txt_TrackStatus.Text = "false"
        Else ' Sinon = Commencer l'enregistrement

            TurnAutomaticPowerOff(False)

            'Initialisation de l'heure de commencement du Track
            frmParamGPS.oldDateTimeTrackRec = Now

            'Test si le GPS est parametré
            ' If frmParamGPS1.lblSécurité.Text = DirectCast(My.Resources.Images.lblGPSStateNoOpen, String) Then
            If frmParamGPS.blGpsON = False Then

                MsgBox(DirectCast(My.Resources.Images.alertNoGPS, String), MsgBoxStyle.Exclamation, "Pocket eRelevé")
            Else
                Me.Txt_TrackStatus.Text = "true"
            End If
        End If
    End Sub

    Private Sub BtnClearTrack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClearTrack.Click
        If (MsgBox(My.Resources.Images.alertTrackClear, MsgBoxStyle.OkCancel, "Pocket eRelevé") = MsgBoxResult.Ok) Then
            Me.lb_Track.Text = ""
        End If
    End Sub

    Private Sub chbBatteryIddle_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbBatteryIddle.Click
        If Me.Txt_TrackStatus.Text = "true" Then
            TurnAutomaticPowerOff(True)
        Else
            TurnAutomaticPowerOff(False)
        End If
    End Sub

    Private Sub TurnAutomaticPowerOff(ByVal bEnable As Boolean)

        Dim regKey As RegistryKey
        Dim stat As Decimal

        regKey = Registry.LocalMachine.OpenSubKey("System\CurrentControlSet\Control\Power\Timeouts", True)
        stat = regKey.GetValue("BattSuspendTimeout")

        If bEnable Then
            ' Active la mis en veille automatique
            chbBatteryIddle.Checked = True
            regKey.SetValue("BattSuspendTimeout", 120, Microsoft.Win32.RegistryValueKind.DWord)
        Else
            ' Desactive la mis en veille automatique
            chbBatteryIddle.Checked = False
            regKey.SetValue("BattSuspendTimeout", 0, Microsoft.Win32.RegistryValueKind.DWord)
        End If

        regKey.Close()

    End Sub

    Private Sub Txt_TrackStatus_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txt_TrackStatus.TextChanged
        If Me.Txt_TrackStatus.Text = "false" Then
            ChangeCtlLightTrackStatus(CtlLightGPSTrack.GpsLightStatus.statusOff)
            Me.BtnStopSaveTrack.Image = startRec
        Else
            ChangeCtlLightTrackStatus(CtlLightGPSTrack.GpsLightStatus.statusOn)
            Me.BtnStopSaveTrack.Image = stopRec
        End If
    End Sub

End Class